<?php

class MappingController extends BaseController
{
	/**
	 * Mapping入口
	 */
	public function output()
	{
		#dd("<h1>duang!</h1>");
		#查找自赠ID
		//$indexParams  = array();
		//$indexParams['index'] = 'udo_index';
		//$indexParams['type'] = 'udo_model';
		//$result = ES_IndexService::instance()->getInsertId($indexParams,'model_id');
		//dd($result);
		//#迁移数据库
//		$result = $this->snapData('udo_item_bak','udo_item','item_id');
//		dd($result);
		////#删除索引
		//$this->deleteIndex();
		//#创建索引
		//$this->createIndex();
		//#查看Mapping
		//$result = $this->getMappingSet();
		//dd($result);
//		$result = $this->putMapping('answers', $this->getAnswerMapping());
//		dd($result);
		//$this->putData();
	}

	public function deleteIndex()
	{
		$indexParams = array();
		$indexParams['index'] = 'udo_index';

		return  ES_IndexService::instance()->deleteESIndex($indexParams);
	}

	public function snapData($old_type,$new_type,$primaryId)
	{
		set_time_limit(0);
		$indexParams = array();
		$indexParams['index'] = 'udo_index';
		$indexParams['type'] = $old_type;
		$indexParams['body']['size'] = 114;
		$result = ES_IndexService::instance()->searchESData($indexParams);
		$indexParams['type'] = $new_type;
		$insertId = ES_IndexService::instance()->getInsertId($indexParams,$primaryId);
		foreach($result['hits']['hits'] as $i =>  $row)
		{
			$indexParams['body'][] =[
				'index' => array(
					'_id' => $insertId
				)
			];
			$row['_source'][$primaryId] = $insertId;
			$indexParams['body'][] = $row['_source'];
			$insertId++;
		}
		unset($indexParams['body']['size']);
		return ES_IndexService::instance()->bulkESData($indexParams);
	}

	public function putData()
	{
		$mVideo        = new Involved_VideoModel();
		$mVideo->setLimit(0,10);
		$result = $mVideo->fetchAll(array(),array(),array('id'=>'asc'));

		$indexParams = array();
		$indexParams['index'] = 'udo_index';
		$indexParams['type'] = 'udo_video';
		$insertId = 1;
		foreach($result as $i =>  $row)
		{
			$indexParams['body'][] =[
				'index' => array(
					'_id' => $insertId
				)
			];
			$row = (array) $row;
			$data = array(
				'video_id' 			=> $row['id'],
				'video_title' 		=> $row['title'],
				'file_name' 		=> $row['name'],
				'video_type'		=> $row['type'],
				'time_length' 		=> $row['time'],
				'view_num' 			=> $row['view_num'],
				'like_num' 			=> $row['like_num'],
				'dislike_num' 		=> $row['dislike_num'],
				'create_user' 		=> $row['user_id'] = 196,
				'user_create_time' 	=> $row['user_create_time'],
				'create_time' 		=> $row['create_time'],
			);
			$indexParams['body'][] = $data;
			$insertId++;
		}
		$result = ES_IndexService::instance()->bulkESData($indexParams);
		dd($result);
	}

	public function createIndex()
	{
		$indexParams = array();
		$indexParams['index']  = 'udo_index';
		#shards & replicas
		$indexParams['body']['settings']['number_of_shards']   = 5;
		$indexParams['body']['settings']['number_of_replicas'] = 1;
		#Mapping
		$itemMapping = $this->getItemMapping();
		$modelMapping = $this->getModelMapping();
		$homeworkMapping = $this->getHomeworkMapping();
		$indexParams['body']['mappings']['udo_item'] = $itemMapping;
		$indexParams['body']['mappings']['udo_model'] = $modelMapping;
		$indexParams['body']['mappings']['udo_homework'] = $homeworkMapping;
		$result = ES_IndexService::instance()->createESIndex($indexParams);
		//ES_ESService::printfJson($indexParams);
		return $result;
	}


    public function getUsersMapping()
    {
        $mapping = array(
            '_source' => array(
                'enabled' => true
            ),
            'properties' => array(
                'user_id' => array(
                    'type' => 'long',
                    'store' => 'yes',
                    'precision_step' => "1"
                ),
                'open_id' => array(
                    'type' => 'string',
                    'store' => 'no',
                    'index' => "analyzed"
                ),
                'phone' => array(
                    'type' => 'long',
                    'store' => 'yes',
                    'precision_step' => "1"
                ),
                'gender' => array(
                    'type' => 'long',
                    'store' => 'yes',
                    'precision_step' => "1"
                ),
                'nickname' => array(
                    'type' => 'string',
                    'store' => 'no',
                    'index' => "analyzed"
                ),
                'real_name' => array(
                    'type' => 'string',
                    'store' => 'no',
                    'index' => "analyzed"
                ),
                'address' => array(
                    'type' => 'string',
                    'store' => 'no',
                    'index' => "analyzed"
                ),
                'nested_friends' => array(
                    'type' => 'nested',
                    'properties' => array(
                        'user_id' => array(
                            'type' => 'long',
                            'store' => 'yes',
                            'precision_step' => "1"
                        )
                    )
                ),
                'nested_circles' => array(
                    'type' => 'nested',
                    'properties' => array(
                        'circle_id' => array(
                            'type' => 'long',
                            'store' => 'yes',
                            'precision_step' => "1"
                        )
                    )
                ),
                'create_time' => array(
                    'type' => 'date',
                    'store' => 'yes',
                    'format' => "yyyy-MM-dd HH:mm:ss"
                )
            )
        );

        return $mapping;
    }

    public function getCircleMapping()
    {
        $mapping = array(
            '_source' => array(
                'enabled' => true
            ),
            'properties' => array(
                'circle_id' => array(
                    'type' => 'long',
                    'store' => 'yes',
                    'precision_step' => "1"
                ),
                'circle_name' => array(
                    'type' => 'string',
                    'store' => 'no',
                    'index' => "analyzed"
                ),
                'merchant_id' => array(
                    'type' => 'long',
                    'store' => 'yes',
                    'precision_step' => "1"
                ),
                'nested_tag_questions' => array(
                    'type' => 'nested',
                    'properties' => array(
                        'tag_q_id' => array(
                            'type' => 'long',
                            'store' => 'yes',
                            'precision_step' => "1"
                        ),
                        'tag_q' => array(
                            'type' => 'string',
                            'store' => 'no',
                            'index' => "analyzed"
                        )
                    )
                ),
                'nested_circle_questions' => array(
                    'type' => 'nested',
                    'properties' => array(
                        'q_id' => array(
                            'type' => 'long',
                            'store' => 'yes',
                            'precision_step' => "1"
                        ),
                        'q' => array(
                            'type' => 'string',
                            'store' => 'no',
                            'index' => "analyzed"
                        )
                    )
                ),
                'nested_users' => array(
                    'type' => 'nested',
                    'properties' => array(
                        'user_id' => array(
                            'type' => 'long',
                            'store' => 'yes',
                            'precision_step' => "1"
                        ),
                        'answer_id' => array(
                            'type' => 'long',
                            'store' => 'yes',
                            'precision_step' => "1"
                        )
                    )
                ),
                'create_time' => array(
                    'type' => 'date',
                    'store' => 'yes',
                    'format' => "yyyy-MM-dd HH:mm:ss"
                )
            )
        );

        return $mapping;
    }


    public function getAnswerMapping()
    {
        $mapping = array(
            '_source' => array(
                'enabled' => true
            ),
            'properties' => array(
                'circle_id' => array(
                    'type' => 'long',
                    'store' => 'yes',
                    'precision_step' => "1"
                ),
                'user_id' => array(
                    'type' => 'long',
                    'store' => 'yes',
                    'precision_step' => "1"
                ),
                'nested_answers' => array(
                    'type' => 'nested',
                    'properties' => array(
                        'q_id' => array(
                            'type' => 'long',
                            'store' => 'yes',
                            'precision_step' => "1"
                        ),
                        'q' => array(
                            'type' => 'string',
                            'store' => 'no',
                            'index' => "analyzed"
                        ),
                        'q_a' => array(
                            'type' => 'string',
                            'store' => 'no',
                            'index' => "analyzed"
                        )
                    )
                ),
                'create_time' => array(
                    'type' => 'date',
                    'store' => 'yes',
                    'format' => "yyyy-MM-dd HH:mm:ss"
                )
            )
        );

        return $mapping;
    }


	public function getMappingSet()
	{
		$indexParams = array();
		$indexParams['index']  = 'udo_index';
		return ES_IndexService::instance()->getMapping($indexParams);
	}

	public function putMapping($type, $mapping)
	{
		$indexParams = array();
		$indexParams['index']  = 'icircle';
		$indexParams['type']   = $type;
		$indexParams['body'][$type] = $mapping;
		return ES_IndexService::instance()->putMapping($indexParams);
	}
	public function nestedFilter()
	{
		$indexParams                                                                                                                   = array();
		$indexParams['index']                                                                                                          = 'udo_index';
		$indexParams['type']                                                                                                           = 'udo_item';
		$indexParams['body']['query']['filtered']['filter']['and'][0]['term']['basic.app_id']                                          = '';
		$indexParams['body']['query']['filtered']['filter']['and'][1]['term']['basic.account_id']                                      = '';
		$indexParams['body']['query']['filtered']['filter']['and'][2]['range']['current_time']                                         = '';
		$indexParams['body']['query']['filtered']['query']['nested']['path']                                                           = 'nest_thread_breakdowns';
		$indexParams['body']['query']['filtered']['query']['nested']['query']['filtered']['query']['match_all'] = new stdClass();
		$indexParams['body']['query']['filtered']['query']['nested']['query']['filtered']['filter']									   = $filter = 1;
	}
}
